package com.fort.teachermanagementsys.controller;

import com.fort.teachermanagementsys.entity.SysRole;

import com.fort.teachermanagementsys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/system/role")
public class RoleController {

    @Autowired
    private SysRoleService roleService;

    // 查询角色列表
    @PreAuthorize("hasPermission('system:role')")
    @GetMapping("/list")
    public List<SysRole> getRoleList() {
        return roleService.getRoleList();
    }

    // 添加角色
    @PreAuthorize("hasPermission('system:role:add')")
    @PostMapping
    public void addRole(@RequestBody SysRole role) {
        roleService.addRole(role);
    }

    // 更新角色
    @PreAuthorize("hasPermission('system:role:edit')")
    @PutMapping
    public void updateRole(@RequestBody SysRole role) {
        roleService.updateRole(role);
    }

    // 删除角色
    @PreAuthorize("hasPermission('system:role:delete')")
    @DeleteMapping("/{id}")
    public void deleteRole(@PathVariable Integer id) {
        roleService.deleteRole(id);
    }

    // 获取单个角色
    @PreAuthorize("hasPermission('system:role')")
    @GetMapping("/{id}")
    public SysRole getRoleById(@PathVariable Integer id) {
        return roleService.getRoleById(id);
    }

    // 分配菜单权限
    @PreAuthorize("hasPermission('system:role:assign')")
    @PostMapping("/assignMenus")
    public void assignMenus(@RequestParam Integer roleId, @RequestBody List<Integer> menuIds) {
        roleService.assignMenus(roleId, menuIds);
    }

    // 获取角色关联的菜单 ID
    @PreAuthorize("hasPermission('system:role')")
    @GetMapping("/{id}/menuIds")
    public List<Integer> getMenuIdsByRoleId(@PathVariable Integer id) {
        return roleService.getMenuIdsByRoleId(id);
    }
}